<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="tag" prop="tagName">
                <el-input
                        v-model="queryParams.tagName"
                        placeholder="请输入tag"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item label="tag数量" prop="tagCount">
                <el-input
                        v-model="queryParams.tagCount"
                        placeholder="请输入tag数量"
                        clearable
                        @keyup.enter="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['UserTopTags:UserTopTags:export']"
                >导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="UserTopTagsList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="用户" align="center" prop="userId" />
            <el-table-column label="tag" align="center" prop="tagName" />
            <el-table-column label="tag数量" align="center" prop="tagCount" />
        </el-table>

        <pagination
                v-show="total>0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
        />

        <!-- 添加或修改用户Toptag对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="UserTopTagsRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="tag" prop="tagName">
                    <el-input v-model="form.tagName" placeholder="请输入tag" />
                </el-form-item>
                <el-form-item label="tag数量" prop="tagCount">
                    <el-input v-model="form.tagCount" placeholder="请输入tag数量" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="UserTopTags">
import { listUserTopTags } from "@/api/UserTopTags/UserTopTags";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const currentUserId = computed(() => userStore.id)

const { proxy } = getCurrentInstance();

const UserTopTagsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        tagName: null,
        tagCount: null
    },
    rules: {
        userId: [
            { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询用户Toptag列表 */
function getList() {
    loading.value = true;
    data.queryParams.userId = currentUserId.value;
    console.log("Query Params:", queryParams.value);
    listUserTopTags(queryParams.value).then(response => {
        UserTopTagsList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        userId: null,
        tagName: null,
        tagId: null,
        tagCount: null
    };
    proxy.resetForm("UserTopTagsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.userId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('UserTopTags/UserTopTags/export', {
        ...queryParams.value
    }, `UserTopTags_${new Date().getTime()}.xlsx`)
}

getList();
</script>
